{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Problem 13"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ein = 0.055\n",
      "Eout = 0.052\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "from numpy.linalg import inv\n",
    "import matplotlib.pyplot as plt\n",
    "\n",
    "#### Problem 13\n",
    "#预处理函数\n",
    "def preprocess(data):\n",
    "    \"\"\"\n",
    "    数据预处理\n",
    "    \"\"\"\n",
    "    #获取维度\n",
    "    n, d = data.shape\n",
    "    #分离X\n",
    "    X = data[:, :-1]\n",
    "    #添加偏置项1\n",
    "    X = np.c_[np.ones(n), X]\n",
    "    #分离y\n",
    "    y = data[:, -1]\n",
    "    \n",
    "    return X, y\n",
    "\n",
    "#计算w\n",
    "def w(X, y, Lambda):\n",
    "    \"\"\"\n",
    "    计算岭回归的结果\n",
    "    \"\"\"\n",
    "    d = X.shape[1]\n",
    "    w = inv(X.T.dot(X) + Lambda * np.eye(d)).dot(X.T).dot(y)\n",
    "    return w\n",
    "\n",
    "#计算误差\n",
    "def E(X, y, w):\n",
    "    return np.mean(np.sign(X.dot(w)) != y)\n",
    "\n",
    "#读取数据\n",
    "train = np.genfromtxt('hw4_train.dat')\n",
    "test = np.genfromtxt('hw4_test.dat')\n",
    "X_train, y_train = preprocess(train)\n",
    "X_test, y_test = preprocess(test)\n",
    "#计算\n",
    "w1 = w(X_train, y_train, 11.26)\n",
    "Ein = E(X_train, y_train, w1)\n",
    "Eout = E(X_test, y_test, w1)\n",
    "print(\"Ein = {}\".format(Ein))\n",
    "print(\"Eout = {}\".format(Eout))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Problem 14"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEU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3X31e6+mmTwXFmKgT8TM9sC/DfgZncfCLqeOdgBrDOzDjOrIzloY1vANc2JJVsQ3wBecPcvFnJfoWnpn4+Z/T7wt0Ab8EMze9bdb3T3nWb2LWAXyT9f/8zdx4OsdZb+BPiKmdUAQ8CdAdeTi63AVjP7LTAC3BHClmW5+SpQD2xP/dXylLt/KNiSsufuY2Z2F/AYUA1sdfedAZc1V68D3gs8b2bPppb9pbs/mu8daRoGEZEKUu7dOyIikkGhLyJSQRT6IiIVRKEvIlJBFPoiIhVEoS8iUkEU+iIiFUShL5IFM7vMzF5KTSchEloKfZEsuPvzJC/z/+OgaxHJhUJfJHvHSN6QRyS0FPoi2bsPqDezC4MuRGSuFPoiWUjNSNkE/BC19iXEFPoiMzCzBpL3J/5T4Hng0mArEpk7hb7IzD4FfNPd96HQl5BT6Iuch5ltAK4HvpxapNCXUNN8+iIiFUQtfRGRCqLQFxGpIAp9EZEKotAXEakgCn0RkQqi0BcRqSAKfRGRCvL/AbJ0ekcRQg2bAAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小Ein对应的Lambda为log10(-8)，对应的Eout为0.02\n"
     ]
    }
   ],
   "source": [
    "#### Problem 14\n",
    "Lambda = range(2, -11, -1)\n",
    "Ein=[]\n",
    "Eout=[]\n",
    "for l in Lambda:\n",
    "    l1 = 10 ** (l)\n",
    "    w1 = w(X_train, y_train, l1)\n",
    "    e_in = E(X_train, y_train, w1)\n",
    "    e_out = E(X_test, y_test, w1)\n",
    "    Ein.append(e_in)\n",
    "    Eout.append(e_out)\n",
    "    \n",
    "plt.plot(Lambda, Ein, label='Ein')\n",
    "plt.xlabel('$\\lambda$')\n",
    "plt.title('Ein')\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n",
    "Lambda1 = Lambda[np.argmin(Ein)]\n",
    "Eout1 = Eout[np.argmin(Ein)]\n",
    "print(\"最小Ein对应的Lambda为log10({})，对应的Eout为{}\".format(Lambda1, Eout1))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Problem 15"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEYCAYAAACwQCa4AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAHRdJREFUeJzt3XuUnHWd5/H3p+9JOgmdpEEhId0BokYjQQPO4DDochHHlegqI8w4wuoZBpVZHWV28LKyMmd2WZ3V2R1RYcd4VrzgBdF4BpdhGHHc5aDpQEADgk1XkTRB0tUJkFSnO3357h9VFYpOk65Od9dTl8/rnDpdz62e70OoT//6V7/6PYoIzMysPjQkXYCZmZWPQ9/MrI449M3M6ohD38ysjjj0zczqiEPfzKyOOPTNzOqIQ9/qlqS0pIOSDhQ9vjDL1/zPkr4+VzWazbWmpAswS9hbI+Kfky7CrFzc0jebRFKDpE9KekLSHklfk7Q0v+0Nkvon7Z+WdL6ki4CPA+/K/9XwYBL1mx2NQ9/sSFfkH28E1gDtwLTdPhHxf4D/Anw7Itoj4vR5rNHsmLh7x+rdDySNFS3/JXAp8LmI6AOQ9DHgV5L+fRIFms0lt/St3r0tIo4revwv4ETgiaJ9niDXQDohkQrN5pBD3+xIu4HVRcsnA2PA00AWWFjYIKkR6Cza19PWWkVz6Jsd6VvAX0jqltTO8/30Y8BjQJukt0hqBj4JtBYd+zTQJcnvLatI/h/T6t2PJo3Tvx3YDNwC/CuQAoaBPweIiGeBDwD/ADxJruVfPJrnu/mfg5LuL9M1mJVMvomKmVn9cEvfzKyOOPTNzOqIQ9/MrI449M3M6kjFf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qPj390xIaiYX+N+IiO/P13lqvXtnC3CppFZJ3cBpwC8SrmkmdgPn5p//G+A3CdYyWz8gdw1IWgu0UIUzI0bELyPi+IjoiogucsHzmkoM/OlIugj4K+DiiBhKup5jsBU4TVK3pBZygza2JFzTMVGuBfEV4JGI+Nx8nqtqWvpHI+ntwN8DncA/StoeEW+KiB2SvgM8TO7P1w9GxHiStc7QnwL/Q1ITMAxcmXA9s7EZ2CzpV8Ah4PIqbFnWmi8ArcBd+b9a7ouIq5ItqXQRMSbpauBOoBHYHBE7Ei7rWL0e+BPgl5K259d9PCLumOsTeRoGM7M6UuvdO2ZmVsShb2ZWRxz6ZmZ1xKFvZlZHHPpmZnXEoW9mVkcc+mZmdcShb1YCSeslPZGfTsKsajn0zUoQEb8k9zX/9yRdi9lsOPTNSreH3A15zKqWQ9+sdDcArZJWJ12I2bFy6JuVID8j5SLgH3Fr36qYQ99sGpLayN2f+APAL4FXJVuR2bFz6JtN75PA1yIijUPfqpxD3+woJL0MuAD4u/wqh75VNc+nb2ZWR9zSNzOrIw59M7M64tA3M6sjDn0zszri0DczqyMOfTOzOuLQNzOrI/8fhY1Kec/utZ0AAAAASUVORK5CYII=\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小Eout对应的Lambda为log10(-7)，对应的Eout为0.015\n"
     ]
    }
   ],
   "source": [
    "#### Problem 15\n",
    "plt.plot(Lambda, Eout, label='Eout')\n",
    "plt.xlabel('$\\lambda$')\n",
    "plt.title('Eout')\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n",
    "Lambda2 = Lambda[np.argmin(Eout)]\n",
    "Eout2 = Eout[np.argmin(Eout)]\n",
    "print(\"最小Eout对应的Lambda为log10({})，对应的Eout为{}\".format(Lambda2, Eout2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Problem 16"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEYCAYAAACwQCa4AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3XuUnXV97/H3J5PLkEwSQmYC5MYEEpSEQIAhqK1YlEuoQjzeiBaFVQ4RIdXqsS2oK2h6Q7SWeoRqilm2HjAiWprWWAqa1rrKJQNEMAlhDwGScXD25AKZnWQyt+/5Y/aE7WSS2ZOZ2dfPa61Z7OfZv2fv7wPMZz/ze37791NEYGZm5WFUvgswM7PcceibmZURh76ZWRlx6JuZlRGHvplZGXHom5mVEYe+WRYk/YGk/8h3HWZDJY/TN3uDpJeBk4GujN3fiYgV+anIbHiNzncBZgXoqoh4NN9FmI0Ed++YZUHS9ZJ+kbEdkm6SlJC0V9LdkpTPGs2y4dA3O37vAS4EzgU+BFyR33LMBubQNzvSQ5Jey/i58Sjt7oiI1yJiB7ABWJTDGs2Oi0Pf7EjvjYgTM37+4SjtfpPx+ABQlYPazIbEoW9mVkYc+mZmZcShb3akf5WUyvj553wXZDZc/OUsM7My4it9M7My4tA3MysjDn0zszLi0DczKyMFN+FadXV11NbW5rsMM7Oi8tRTT+2KiJqB2hVc6NfW1lJfX5/vMszMioqkV7Jpl1X3jqQlkrZJapB0az/P3yTpOUmbJP1C0vz0/lpJB9P7N0n65uBOw8zMhtOAV/qSKoC7gcuARmCjpHURsSWj2f0R8c10+6uBrwFL0s+9GBGeiMrMrABkc6W/GGiIiO0R0Q6sBZZmNoiIfRmbEwB/48vMrABl06c/A9iZsd0IXNS3kaRbgM8AY4F3Zjw1R9IzwD7gCxHx3/0cuxxYDjB79uwjCujo6KCxsZG2trYsyi1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},
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小Etrain对应的Lambda为log10(-8)，对应的Eout为0.025\n"
     ]
    }
   ],
   "source": [
    "####Problem 16\n",
    "X_train_ = X_train[:120, :]\n",
    "y_train_ = y_train[:120]\n",
    "X_val = X_train[120:, :]\n",
    "y_val = y_train[120:]\n",
    "\n",
    "Etrain = []\n",
    "Eout = []\n",
    "Eval = []\n",
    "for l in Lambda:\n",
    "    l1 = 10 ** l\n",
    "    w1 = w(X_train_, y_train_, l1)\n",
    "    e_train = E(X_train_, y_train_, w1)\n",
    "    e_out = E(X_test, y_test, w1)\n",
    "    e_val = E(X_val, y_val, w1)\n",
    "    Etrain.append(e_train)\n",
    "    Eout.append(e_out)\n",
    "    Eval.append(e_val)\n",
    "    \n",
    "plt.plot(Lambda, Etrain, label='Etrain')\n",
    "plt.xlabel('$\\lambda$')\n",
    "plt.title('Ein')\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n",
    "Lambda3 = Lambda[np.argmin(Etrain)]\n",
    "Eout3 = Eout[np.argmin(Etrain)]\n",
    "print(\"最小Etrain对应的Lambda为log10({})，对应的Eout为{}\".format(Lambda3, Eout3))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Problem 17"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEYCAYAAACwQCa4AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xt8lOWd9/HPLwkhHOWQoJVTEIItgqIG6Op6oPUA21WsthWtFretvFyPXdttcdsHW6q7Slvr+qhtqVKf7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M5sFfAs43933BV3PYVgHlJnZODMrJNJpY3nANR0Wi1xB3A+86e539OZ7ZcyV/qGY2WeB/wuUAH80s/Xufq67bzCzXwMbifz6eo27twVZa5KuBP7TzAqAJmB+wPV0x1JgqZn9BWgB5mXglWW2uRvoCzwZ/a1ltbtfFWxJiXP3VjO7FngCyAeWuvuGgMs6XKcClwNvmNn66Lp/c/cVPf1GGoZBRCSHZHvzjoiIxFHoi4jkEIW+iEgOUeiLiOQQhb6ISA5R6IuI5BCFvohIDlHoiyTAzKaY2TvR4SREMpZCXyQB7v4Gka/5fynoWkS6Q6EvkrgaIhPyiGQshb5I4m4D+prZ2KALETlcCn2RBERHpBwA/BFd7UsGU+iLdMHMiojMT3w18AYwOdiKRA6fQl+ka98B/svdt6LQlwyn0Bc5BDM7FjgbuDO6SqEvGU3j6YuI5BBd6YuI5BCFvohIDlHoi4jkEIW+iEgOUeiLiOQQhb6ISA5R6IuI5JD/BYE7/w24Gqv/AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小Eval对应的Lambda为log10(0)，对应的Eout为0.028\n"
     ]
    }
   ],
   "source": [
    "#### Problem 17\n",
    "plt.plot(Lambda, Eval, label='Etrain')\n",
    "plt.xlabel('$\\lambda$')\n",
    "plt.title('Eval')\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n",
    "Lambda4 = Lambda[np.argmin(Eval)]\n",
    "Eout4 = Eout[np.argmin(Eval)]\n",
    "print(\"最小Eval对应的Lambda为log10({})，对应的Eout为{}\".format(Lambda4, Eout4))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Problem 18"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ein = 0.035\n",
      "Eout = 0.02\n"
     ]
    }
   ],
   "source": [
    "#### Problem 18\n",
    "l = Lambda[np.argmin(Eval)]\n",
    "l1 = 10 ** l\n",
    "w1 = w(X_train, y_train, l1)\n",
    "Ein = E(X_train, y_train, w1)\n",
    "Eout = E(X_test, y_test, w1)\n",
    "print(\"Ein = {}\".format(Ein))\n",
    "print(\"Eout = {}\".format(Eout))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Problem 19"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAX0AAAEYCAYAAACwQCa4AAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAHfZJREFUeJzt3XuYXHWd5/H3p7vT6VwIJOmWS64dRDGAgCRhHtwRVALBS4AVMDg6eBlZRdiZ1dmZMPrAgOOzLMx6G5lnYMbouCuGeBk3rrgIipcZN9LNRZiA0ZAqkiYoqW7IpTpJp7u/+0dVxbLppKtvderyeT1PP6lz6pw630PoT5/8zrd/RxGBmZnVh4akCzAzs/Jx6JuZ1RGHvplZHXHom5nVEYe+mVkdceibmdURh76ZWR1x6JsBktKS9kvaV/T1haTrMptoTUkXYFZB3h4RDyZdhNlk8pW+2QgkfVDS05L2SnpK0uskrZX0jSHbfU7S55Oq06wUDn2zo5B0JfDXwB8Ds4DVQDfwNeAtkmblt2sErgLuSaZSs9J4eMfsd74tqb9o+b8CVwC3R0RHft3WwpuSHgUuA74CvAnojYhN5SrWbCx8pW/2O5dFxHFFX/8ILACeOcL29wBX51+/C1/lWxVw6Jsd3Q7g5CO893XgAknzgctx6FsVcOibHd0/AX8u6RzlvFLSIoCI2AX8CPgSkIqIpxOs06wkHtM3+53vSBooWn4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 432x288 with 1 Axes>"
      ]
     },
     "metadata": {
      "needs_background": "light"
     },
     "output_type": "display_data"
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "最小Ecv对应的Lambda为log10(-8)\n"
     ]
    }
   ],
   "source": [
    "####Problem 19\n",
    "#准备数据\n",
    "K = 5\n",
    "m = X_train.shape[0] // K\n",
    "data=[]\n",
    "#将数据拆分成训练集以及验证集\n",
    "for i in range(K):\n",
    "    Xtrain = np.r_[X_train[: i*m], X_train[(i+1)*m: ]]\n",
    "    ytrain = np.r_[y_train[: i*m], y_train[(i+1)*m: ]]\n",
    "    Xval = X_train[i*m: (i+1)*m]\n",
    "    yval = y_train[i*m: (i+1)*m]\n",
    "    data.append([Xtrain, ytrain, Xval, yval])\n",
    "\n",
    "Ecv = []\n",
    "for l in Lambda:\n",
    "    l1 = 10 ** l\n",
    "    ecv = 0\n",
    "    for d in data:\n",
    "        Xtrain, ytrain, Xval, yval = d\n",
    "        w1 = w(Xtrain, ytrain, l1)\n",
    "        ecv += E(Xval, yval, w1)\n",
    "    ecv /= K\n",
    "    Ecv.append(ecv)\n",
    "    \n",
    "plt.plot(Lambda, Ecv, label='Ecv')\n",
    "plt.xlabel('$\\lambda$')\n",
    "plt.title('Ecv')\n",
    "plt.legend()\n",
    "plt.show()\n",
    "\n",
    "Lambda5 = Lambda[np.argmin(Ecv)]\n",
    "print(\"最小Ecv对应的Lambda为log10({})\".format(Lambda5))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### Problem 20"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Ein = 0.015\n",
      "Eout = 0.02\n"
     ]
    }
   ],
   "source": [
    "####Problem20\n",
    "l1 = 10 ** Lambda5\n",
    "w1 = w(X_train, y_train, l1)\n",
    "Ein = E(X_train, y_train, w1)\n",
    "Eout = E(X_test, y_test, w1)\n",
    "print(\"Ein = {}\".format(Ein))\n",
    "print(\"Eout = {}\".format(Eout))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
